CP=cp
GZIP=gzip
GZIP_OPTS=-9 -f
DPKG=dpkg-deb
FAKEROOT=fakeroot
LINTIAN=lintian

all:
	$(MAKE) deb
	$(MAKE) tgz
	
tgz:
	tar cfz out/simple-backup-0.2.tar.gz src/scripts/* src/doc/*		

deb: out/*.deb

out/*.deb: out/build/DEBIAN/control out/build/usr/bin/simple-backup out/build/usr/share/man/man1/simple-backup.1.gz\
out/build/usr/share/doc/simple-backup/copyright out/build/usr/share/doc/simple-backup/changelog.Debian.gz
	${FAKEROOT} ${DPKG} --build out/build out/

validate: out/*.deb
	${LINTIAN} out/*.deb

out/build/DEBIAN/control: src/deb/control
	$(MAKE) prepare
	${CP} src/deb/control out/build/DEBIAN

out/build/usr/bin/simple-backup: src/scripts/simple-backup
	$(MAKE) prepare
	${CP} src/scripts/simple-backup out/build/usr/bin/simple-backup

out/build/usr/share/man/man1/simple-backup.1.gz: out/build/usr/share/man/man1/simple-backup.1
	$(MAKE) prepare
	${GZIP} ${GZIP_OPTS} out/build/usr/share/man/man1/simple-backup.1

out/build/usr/share/man/man1/simple-backup.1: src/doc/simple-backup.1
	$(MAKE) prepare
	${CP} src/doc/simple-backup.1 out/build/usr/share/man/man1/simple-backup.1

out/build/usr/share/doc/simple-backup/copyright: src/doc/copyright
	$(MAKE) prepare
	${CP} src/doc/copyright out/build/usr/share/doc/simple-backup/copyright

out/build/usr/share/doc/simple-backup/changelog.Debian.gz: CHANGELOG
	$(MAKE) prepare
	${GZIP} ${GZIP_OPTS} -c CHANGELOG > out/build/usr/share/doc/simple-backup/changelog.Debian.gz
	fakeroot chmod 644 out/build/usr/share/doc/simple-backup/changelog.Debian.gz

clean:
	rm -rf out
	
prepare:
	mkdir -p out/build/DEBIAN
	mkdir -p out/build/usr/bin
	mkdir -p out/build/usr/share/man/man1
	mkdir -p out/build/usr/share/doc/simple-backup
	fakeroot chmod 755 out/build/usr
	fakeroot chmod 755 out/build/usr/bin
	fakeroot chmod 755 out/build/usr/share
	fakeroot chmod 755 out/build/usr/share/man
	fakeroot chmod 755 out/build/usr/share/man/man1
	fakeroot chmod 755 out/build/usr/share/doc
	fakeroot chmod 755 out/build/usr/share/doc/simple-backup
